package com.ruoyi.business.service.impl;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import com.ruoyi.business.vo.*;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.WxFaceDeviceMapper;
import com.ruoyi.business.domain.WxFaceDevice;
import com.ruoyi.business.service.IWxFaceDeviceService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 人脸设别Service业务层处理
 * 
 * @author asher
 * @date 2021-11-27
 */
@Service
public class WxFaceDeviceServiceImpl implements IWxFaceDeviceService 
{
    @Autowired
    private WxFaceDeviceMapper wxFaceDeviceMapper;

    @Autowired
    @Qualifier("HikVisionService")
    private HikVisionServiceImpl hikVisionService;

    /**
     * 查询人脸设别
     * 
     * @param faceDeviceId 人脸设别主键
     * @return 人脸设别
     */
    @Override
    public WxFaceDevice selectWxFaceDeviceByFaceDeviceId(Long faceDeviceId)
    {
        return wxFaceDeviceMapper.selectWxFaceDeviceByFaceDeviceId(faceDeviceId);
    }

    /**
     * 查询人脸设别列表
     * 
     * @param wxFaceDevice 人脸设别
     * @return 人脸设别
     */
    @Override
    public List<WxFaceDevice> selectWxFaceDeviceList(WxFaceDevice wxFaceDevice)
    {
        return wxFaceDeviceMapper.selectWxFaceDeviceList(wxFaceDevice);
    }

    /**
     * 新增人脸设别
     * 
     * @param wxFaceDevice 人脸设别
     * @return 结果
     */
    @Override
    public int insertWxFaceDevice(WxFaceDevice wxFaceDevice)
    {
        wxFaceDevice.setCreateTime(DateUtils.getNowDate());
        // 如果是海康设备 默认是千禧设备
        if (WxDeviceConstants.HIK_DEVICE.equals(wxFaceDevice.getDeviceType())) {
            HikDeviceInfo hikDeviceInfo = new HikDeviceInfo();
            hikDeviceInfo.setDevType("AccessControl");
            hikDeviceInfo.setEhomeParams(new EhomeParams(wxFaceDevice.getHikDeviceNum(), wxFaceDevice.getHikDevicePassword()));
            hikDeviceInfo.setDevName(wxFaceDevice.getName());
            hikDeviceInfo.setProtocolType("ehomeV5");
            DeviceInfoOutList deviceInfoOutList = null;
            try {
                deviceInfoOutList = hikVisionService.addDevice(hikDeviceInfo);
            } catch (Exception e) {
                e.printStackTrace();
                return 0;
            }
            List<DeviceOut> deviceOutList = deviceInfoOutList.getDeviceOutList();
            if (deviceOutList.size() == 0) {
                return 0;
            }
            Device device = deviceOutList.get(0).getDevice();
            wxFaceDevice.setHikDeviceIndex(device.getDevIndex());
        }
        return wxFaceDeviceMapper.insertWxFaceDevice(wxFaceDevice);
    }

    /**
     * 修改人脸设别
     * 
     * @param wxFaceDevice 人脸设别
     * @return 结果
     */
    @Override
    public int updateWxFaceDevice(WxFaceDevice wxFaceDevice)
    {
        wxFaceDevice.setUpdateTime(DateUtils.getNowDate());
        return wxFaceDeviceMapper.updateWxFaceDevice(wxFaceDevice);
    }

    /**
     * 批量删除人脸设别
     * 
     * @param faceDeviceIds 需要删除的人脸设别主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteWxFaceDeviceByFaceDeviceIds(Long[] faceDeviceIds)
    {
        int i = 0;
        try {
            List<Long> longs = Arrays.asList(faceDeviceIds);
            for (Long item : longs) {
                WxFaceDevice wxFaceDevice = wxFaceDeviceMapper.selectWxFaceDeviceByFaceDeviceId(item);
                if (WxDeviceConstants.HIK_DEVICE.equals(wxFaceDevice.getDeviceType())) {
                    Boolean result = hikVisionService.deleteDevice(wxFaceDevice.getHikDeviceIndex());
                    if (result) {
                        i = i + wxFaceDeviceMapper.deleteWxFaceDeviceByFaceDeviceId(item);
                    }
                }
                i = i + wxFaceDeviceMapper.deleteWxFaceDeviceByFaceDeviceId(item);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return i;
    }

    /**
     * 删除人脸设别信息
     * 
     * @param faceDeviceId 人脸设别主键
     * @return 结果
     */
    @Override
    public int deleteWxFaceDeviceByFaceDeviceId(Long faceDeviceId)
    {
        return wxFaceDeviceMapper.deleteWxFaceDeviceByFaceDeviceId(faceDeviceId);
    }

    @Override
    public List<WxFaceDevice> selectWxFaceDeviceByVillageId(Long villageId) {
        return wxFaceDeviceMapper.selectWxFaceDeviceByVillageId(villageId);
    }
}
